package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyCloudServiceCastMessagePageDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyCloudServiceCostSaveOrderDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyInvoiceInfoDTO;
import com.itlong.cloud.POJO.PO.PropertyCloudServiceCostOrderInfoPO;
import com.itlong.cloud.POJO.PO.PropertyInvoiceInfoPO;
import com.itlong.cloud.POJO.VO.property.PropertyCloudServiceCostPaymentByPageVO;
import com.itlong.cloud.POJO.VO.property.PropertyCloudServiceCostRecordByPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.PaymentTypeEnum;
import com.itlong.cloud.property.dao.IPropertyCloudServiceCostDao;
import com.itlong.cloud.property.service.IPropertyCloudServiceCostService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.page.PageByVisitor;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <desc>
 * 云服务费支付业务逻辑处理层
 * </desc>
 *
 * @createDate 2019/8/12
 */
@Service
public class PropertyCloudServiceCostServiceImpl implements IPropertyCloudServiceCostService {
    private static Logger LOG = LoggerFactory.getLogger(PropertyCloudServiceCostServiceImpl.class);

    @Autowired
    IPropertyCloudServiceCostDao iPropertyCloudServiceCostDao;

    /**
     * <desc>
     *      获取云服务费的订单列表。
     * <desc/>
     *
     * @param  projectId 项目ID
     * @param  currentPage 当前页
     * @param  pageSize 页面大小
     * @param  feeStatus 查询条件 1待缴费 2全部
     * @return
     * @author Juguang.S
     * @createDate 2019-11-22
     */
    @Override
    public PageByVisitor<PropertyCloudServiceCostPaymentByPageVO> getOrderInfo(String projectId, String currentPage, String pageSize,String feeStatus,String orderId,String orderColumn,String orderRule) throws Exception {

        //将DTO转换为Map params
        Map<String, Object> params = new HashMap<>();
        params.put("projectId",projectId);
        params.put("currentPage",currentPage);
        params.put("pageSize",pageSize);
        params.put("feeStatus",feeStatus);
        params.put("orderId",orderId);
        params.put("orderColumn", StringUtils.isEmpty(orderColumn)?"id":orderColumn);
        params.put("orderRule", StringUtils.isEmpty(orderRule)?"DESC":orderRule);

        //分页类默认pageSize为20
        PageByVisitor<PropertyCloudServiceCostPaymentByPageVO> page = new PageByVisitor();
        page.getPage().setPageSize(pageSize);
        page.getPage().setCurrentPage(currentPage);
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        List<PropertyCloudServiceCostPaymentByPageVO> list = iPropertyCloudServiceCostDao.getPaymentListByPage(params);
        if(list != null){
            for(PropertyCloudServiceCostPaymentByPageVO cloudServiceCostMessageVO : list){
                //未支付过期时间取上一条已支付的过期时间进行计算 针对无多条复用的情况
                if(cloudServiceCostMessageVO.getFeeStatus().equals("1") && StringUtils.isEmpty(cloudServiceCostMessageVO.getOldBatchNumber())){
                    cloudServiceCostMessageVO.setEndDays(iPropertyCloudServiceCostDao.getCloudServiceCostEndTime(cloudServiceCostMessageVO.getBatchNumber()));
                }
                //未支付过期时间取旧批次号过期时间最早的一条已支付的过期时间进行计算 针对有多条复用的情况
                if(cloudServiceCostMessageVO.getFeeStatus().equals("1") && StringUtils.isNotEmpty(cloudServiceCostMessageVO.getOldBatchNumber())){
                    cloudServiceCostMessageVO.setEndDays(iPropertyCloudServiceCostDao.getCloudServiceCostEndTimeBySome(cloudServiceCostMessageVO.getOldBatchNumber().split(",")));
                }
                cloudServiceCostMessageVO.setChargeDetail(iPropertyCloudServiceCostDao.getOrderDataInfo(cloudServiceCostMessageVO.getOrderId()));
                cloudServiceCostMessageVO.setLastAmount(iPropertyCloudServiceCostDao.getLastAmountbyBatchNumber(cloudServiceCostMessageVO.getBatchNumber()));
            }
        }
        page.setRows(list, iPropertyCloudServiceCostDao.getPaymentByPageCount(params));
        page.setVisitorCount(iPropertyCloudServiceCostDao.getNotPaymentCount(projectId));
        return page;
    }

    /**
     * <desc>
     * 修改线下银行支付订单
     * <desc/>
     *
     * @param orderDTO 订单
     * @return
     * @author Juguang.S
     * @createDate 2019/8/12
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String saveOrder(PropertyCloudServiceCostSaveOrderDTO orderDTO) throws Exception {
        PropertyCloudServiceCostOrderInfoPO orderInfoPO = new PropertyCloudServiceCostOrderInfoPO();
        BeanUtils.copyProperties(orderDTO, orderInfoPO);
        orderInfoPO.setPaymentType(PaymentTypeEnum.BANK_PAY.getType());
        orderInfoPO.setFeeStatus(3);
        orderInfoPO.setUpdateTime(new Date());
        orderInfoPO.setPayTime(new Date());
        iPropertyCloudServiceCostDao.update(SqlUtil.durableData(orderInfoPO, PlatformConstants.TABLE_UPDATE));
        return orderInfoPO.getOrderId();
    }

    /**
     * <desc>
     * 获取物业云服务费续费记录分页信息。
     * <desc/>
     *
     * @param pageDTO 请求参数
     * @return
     * @author Juguang.S
     * @createDate 2019-08-12
     */
    @Override
    public Page<PropertyCloudServiceCostRecordByPageVO> getPaymentRecordByPage(PropertyCloudServiceCastMessagePageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<PropertyCloudServiceCostRecordByPageVO> page = new Page();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iPropertyCloudServiceCostDao.getPaymentRecordByPage(params), iPropertyCloudServiceCostDao.getPaymentRecordByPageCount(params));
        return page;
    }

    /**
     * <desc>
     * 检测是否还有未支付的结下账单
     * <desc/>
     *
     * @param projectId 项目编号
     * @return
     * @author Juguang.S
     * @createDate 2019/8/21
     */
    @Override
    public Integer getIsBankPayOrder(String projectId) {
        return iPropertyCloudServiceCostDao.getIsBankPayOrder(projectId);
    }

    /**
     * <desc>
     *      保存物业发票信息。
     * <desc/>
     *
     * @param  dto 物业发票信息
     * @return
     * @author Juguang.S
     * @createDate 2019-11-22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer addInvoiceInfo(PropertyInvoiceInfoDTO dto) throws Exception {
        PropertyInvoiceInfoPO infoPO = new PropertyInvoiceInfoPO();
        BeanUtils.copyProperties(dto,infoPO);
        if(StringUtils.isNotEmpty(dto.getInvoiceId())){
            infoPO.setUpdateTime(new Date());
            infoPO.setOperateUserId(dto.getLoginUserId());
            iPropertyCloudServiceCostDao.update(SqlUtil.durableData(infoPO,PlatformConstants.TABLE_UPDATE));
        }else{
            infoPO.setCreateTime(new Date());
            infoPO.setInvoiceId(LogicIdUtil.bussinessId());
            infoPO.setCreateUserId(dto.getLoginUserId());
            iPropertyCloudServiceCostDao.save(SqlUtil.durableData(infoPO,PlatformConstants.TABLE_SAVE));
        }
        return iPropertyCloudServiceCostDao.updateInvoiceStastus(dto.getOrderId());
    }

    /**
     * <desc>
     *      根据订单ID查询订单金额及支付状态
     * <desc/>
     *
     * @param  orderId 订单Id
     * @return
     * @author Juguang.S
     * @createDate 2019/11/25
     */
    @Override
    public PropertyCloudServiceCostOrderInfoPO getOrderAmount(String orderId) {
        return iPropertyCloudServiceCostDao.getOrderAmount(orderId);
    }
}